package com.pipayshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.config.RedisConfig;
import com.pipayshop.domain.ShopCommodityEvaluate;
import com.pipayshop.domain.ShopOrder;
import com.pipayshop.domain.dto.ShopCommodityEvaluateAddDto;
import com.pipayshop.domain.vo.PageVo;
import com.pipayshop.domain.vo.ShopCommodityEvaluateVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.mapper.OnlineStoreOrderMapper;
import com.pipayshop.mapper.OnlineStoreShopCommodityEvaluateMapper;
import com.pipayshop.mapper.OnlineStoreShopMapper;
import com.pipayshop.service.OnlineStoreShopCommodityEvaluateService;
import com.pipayshop.utils.Constants;
import com.pipayshop.utils.StringUtil;
import com.pipayshop.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author xiaoqiu
 * @date 2024/9/23 10:29
 * @description
 */
@Service
public class OnlineStoreShopCommodityEvaluateServiceImpl extends ServiceImpl<OnlineStoreShopCommodityEvaluateMapper, ShopCommodityEvaluate> implements OnlineStoreShopCommodityEvaluateService {

    @Autowired
    private OnlineStoreShopCommodityEvaluateMapper shopCommodityEvaluateMapper;
    @Autowired
    private OnlineStoreOrderMapper shopOrderMapper;
    @Autowired
    private OnlineStoreShopMapper shopMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageVo getItemCommodityEvaluates(String commodityId, Integer page, Integer limit) {
        page = (page - 1) * limit;
        List<ShopCommodityEvaluateVO> itemCommodityEvaluates = shopCommodityEvaluateMapper.getItemCommodityEvaluates(commodityId, page, limit);
        Long itemCommodityEvaluatesSum = shopCommodityEvaluateMapper.getShopCommodityEvaluatesSum(commodityId);
        return new PageVo(itemCommodityEvaluatesSum, itemCommodityEvaluates);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addItemEvaluates(ShopCommodityEvaluateAddDto shopCommodityEvaluateAddDto) {
        ValidationUtil.validateString(shopCommodityEvaluateAddDto.getUserId(), "user_id_required");
        ValidationUtil.validateString(shopCommodityEvaluateAddDto.getEvaluate(), "review_content_required");
        ValidationUtil.validateString(shopCommodityEvaluateAddDto.getShopId(), "shop_id_required");
        ValidationUtil.validateString(shopCommodityEvaluateAddDto.getCommodityId(), "commodity_id_required");
        ValidationUtil.validateString(shopCommodityEvaluateAddDto.getOrderId(), "order_id_required");
        String evaluateId = StringUtil.generateShortId();
        //设置网店评分
        shopMapper.setShopScore();
        //删除订单列表缓存
        deleteKeysByPattern(shopCommodityEvaluateAddDto.getUserId());
        int result = shopCommodityEvaluateMapper.insertItemCommodityEvaluateAddVO(
                evaluateId,
                shopCommodityEvaluateAddDto);
        int update = shopOrderMapper.update(null, new UpdateWrapper<ShopOrder>()
                .eq("order_id", shopCommodityEvaluateAddDto.getOrderId())
                .set("order_status", 4));
        if (update < 1){throw new BusinessException("order_update_failed");}
        return result>0;
    }
    public void deleteKeysByPattern(String  userId) {
        // 构建匹配模式
        String pattern = Constants.SHOP_ORDER_LIST_KEY + "::" +userId + "*";

        // 使用 keys 命令匹配所有符合模式的键
        Set<String> keys = redisTemplate.keys(pattern);

        if (keys != null && !keys.isEmpty()) {
            // 批量删除匹配到的键
            redisTemplate.delete(keys);
        }
    }
    @Override
    public boolean deleteItemEvaluates(String evaluateId, String userId) {
        int result = shopCommodityEvaluateMapper.update(null, new UpdateWrapper<ShopCommodityEvaluate>()
                .eq("evaluate_id", evaluateId)
                .eq("user_id", userId)
                .set("status", 1));
        return result > 0;
    }



}
